package com.learn.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.learn.entity.OrdEntity;
import com.learn.service.OrdService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.learn.entity.InsureEntity;
import com.learn.service.InsureService;
import com.learn.utils.PageUtils;
import com.learn.utils.Query;
import com.learn.utils.R;


/**
 * 险种信息
 */
@RestController
@RequestMapping("insure")
public class InsureController extends AbstractController {
    @Autowired
    private InsureService insureService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {


        if (super.getUserId() > 1 && "3".equals(super.getUser().getType())) {
            params.put("state", "上架");
            params.put("user", super.getUserId());
        }
        //查询列表数据
        Query query = new Query(params);

        List<InsureEntity> insureList = insureService.queryList(query);
        int total = insureService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(insureList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 列表
     */
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params) {
        params.put("state", "上架");
        Query query = new Query(params);
        List<InsureEntity> insureList = insureService.queryList(query);
        return R.ok().put("list", insureList);
    }


    @Autowired
    OrdService ordService;

    @RequestMapping("/list3")
    public R list3(@RequestParam Map<String, Object> params) {
        Map<String, Object> params1 = new HashMap<>();
        params1.put("costumer", super.getUserId());
        String types = "";
        for (OrdEntity entity : this.ordService.queryList(params1)) {
            types += entity.getInsureEntity().getType() + ",";
        }

        if (StringUtils.isNotEmpty(types)) {

            types = types.substring(0, types.length() - 1);
            params.put("types", types);
        }
        params.put("state", "上架");
        //查询列表数据
        Query query = new Query(params);

        List<InsureEntity> insureList = insureService.queryList(query);
        int total = insureService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(insureList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        InsureEntity insure = insureService.queryObject(id);

        return R.ok().put("insure", insure);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody InsureEntity insure) {


        insureService.save(insure);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody InsureEntity insure) {
        insureService.update(insure);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        insureService.deleteBatch(ids);

        return R.ok();
    }

}
